package com.czie.myapplication;

import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class DataPacket {
    private String sensorId;
    private int sensorType;
    private String sensorData;
    private static Map<String, String> data = new HashMap<String, String>();

    public DataPacket(String sensorId, int sensorType, String sensorData) {
        this.sensorId = sensorId;
        this.sensorType = sensorType;
        this.sensorData = sensorData;
    }

    public static List<DataPacket> parse(byte[] buf, int serialPort, String gatewayId) {
        List<DataPacket> packets = null;
//		System.out.println("PPPPPARSE serial port " + serialPort);

        if (serialPort == 0x01) { // 开关量，模拟量，主要是工业传感器
            packets = parseIndustrySensorPacket(buf, gatewayId);
        } else if (serialPort == 0x03 || serialPort == 0x04) { // 解析ZIGBEE传感器
            packets = parseZigbeePacket(buf, gatewayId);
        } else if (serialPort == 0x02) { // 解析电表数据
            packets = parsePowerPacket(buf, gatewayId);
//			Log.e("DataPacket", "解析到电表数据");
        }
        return packets;
    }

    private static String bytesToShortString(byte[] buf, int offset) {
        return String.valueOf(ByteIntConvertor.bytesToShort1(buf, offset));
    }

    // 串口1的数据解析，为工业传感器以及几个开关量报警器的采集
    private static List<DataPacket> parseIndustrySensorPacket(byte[] buf, String gatewayId) {
        List<DataPacket> packets = new ArrayList<DataPacket>();
        String sensorId;
        int type;
        int value;
        DataPacket packet;
        switch (buf[0]) {
            case 0x02: // 风向
                byte[] aa = new byte[10];
                System.arraycopy(buf, 0, aa, 0, 6);
//			Log.d("AAAAA", "DDDDD win direction:" + Arrays.toString(buf));
                sensorId = gatewayId + "00" + "010101";
//			packet = new DataPacket(sensorId, Sensor.WIN_DIRECTION_SENSOR, bytesToShortString(buf, 5));
                String directionStr = String.valueOf(buf[4]);
                int direction = Integer.parseInt(directionStr);
                packet = new DataPacket(sensorId, Sensor.WIN_DIRECTION_SENSOR, String.valueOf(direction * 45));
                Log.d("", "DataPacket win_direction value:" + packet.getSensorData());
                packets.add(packet);
                break;
            case 0x01: // 风速
                sensorId = gatewayId + "00010201";
                packet = new DataPacket(sensorId, Sensor.WIND_SPEED_SENSOR, bytesToShortString(buf, 3));
                packets.add(packet);
                break;
            case 0x03: // 开关量报警器状态,4位对应:1111->震荡，烟雾，煤气，红外栅栏
                byte[] vv = new byte[8];
//			System.arraycopy(buf, 0, vv, 0, 6);
                if (buf[1] == 5) {
//				Log.d("ddddd", "iiiii:"+Arrays.toString(vv));
//				Log.d("","DataPacket is control send ");
                    if (buf[3] == 1) { //light1
                        packet = new DataPacket(gatewayId + "0C010302", Sensor.LIGHT2_REALY, String.valueOf(buf[4] == -1 ? 1 : 0));
                        Log.d("", "DataPacket is lightId:" + packet.getSensorId() + "  value = " + String.valueOf(buf[4] == -1 ? 1 : 0));
                        packets.add(packet);
                    } else if (buf[3] == 3) {
                        packet = new DataPacket(gatewayId + "0C010304", Sensor.FAN1_RELAY, String.valueOf(buf[4] == -1 ? 1 : 0));
                        Log.d("", "DataPacket is fanId:" + packet.getSensorId() + "  value = " + String.valueOf(buf[4] == -1 ? 1 : 0));
                        packets.add(packet);
                    } else if (buf[3] == 2) {
                        packet = new DataPacket(gatewayId + "0C010303", Sensor.ALARM_RALAY, String.valueOf(buf[4] == -1 ? 1 : 0));
                        Log.d("", "DataPacket is alarmId:" + packet.getSensorId() + "  value = " + String.valueOf(buf[4] == -1 ? 1 : 0));
                        packets.add(packet);
                    } else if (buf[3] == 0) {
                        packet = new DataPacket(gatewayId + "0C010301", Sensor.LIGHT1_RELAY, String.valueOf(buf[4] == -1 ? 1 : 0));
                        packets.add(packet);
                    }
                } else if (buf[1] == 2) {
                    // TODO:第一路对射，第二三：烟雾，煤气，四：震动，五：人体
                    // TODO:修正：第一路对射，第二三：烟雾，震动，四：煤气，五：人体
                    value = buf[3];
                    sensorId = gatewayId + "00010301";
                    packet = new DataPacket(sensorId, Sensor.LIGHT_BAR_SENSOR, (value & 0x01) == 0 ? "1" : "0");
                    Log.e("报警", "=====红外光栅");
                    packets.add(packet);

                    sensorId = gatewayId + "00010302";
                    packet = new DataPacket(sensorId, Sensor.SMOKE_SENSOR, (value & 0x02) == 0 ? "1" : "0");
                    Log.e("报警", "=====接收到烟雾 value =" + ((value & 0x02) == 0 ? "1" : "0"));

                    packets.add(packet);

                    sensorId = gatewayId + "00010303";
//				packet = new DataPacket(sensorId, Sensor.GAS_SENSOR, (value & 0x03) == 0 ? "1" : "0");
                    packet = new DataPacket(sensorId, Sensor.GLASS_SHAKE_SENSOR, (value & 0x03) == 0 ? "1" : "0");
                    Log.e("报警", "=====接收到震动 value =" + ((value & 0x03) == 0 ? "1" : "0"));
                    packets.add(packet);

                    sensorId = gatewayId + "00010304";
//				packet = new DataPacket(sensorId, Sensor.GLASS_SHAKE_SENSOR, (value & 0x04) == 0 ? "1" : "0");
                    packet = new DataPacket(sensorId, Sensor.GAS_SENSOR, (value & 0x04) == 0 ? "1" : "0");
                    Log.e("报警", "=====接收到可燃气体 value =" + ((value & 0x04) == 0 ? "1" : "0"));
                    packets.add(packet);

                    sensorId = gatewayId + "00010305";
                    packet = new DataPacket(sensorId, Sensor.BODY_SENSOR, (value & 0x05) == 0 ? "1" : "0");
                    Log.e("报警", "=====接收到人体 value =" + ((value & 0x05) == 0 ? "1" : "0"));
                    packets.add(packet);
                }
                break;
            case 0x04: // 模拟量采集器：应力，液压，工业温度，位移
                sensorId = gatewayId + "00010401";
                packet = new DataPacket(sensorId, Sensor.WEIGHT_SENSOR, bytesToShortString(buf, 3));
                packets.add(packet);

                sensorId = gatewayId + "00010402";
                packet = new DataPacket(sensorId, Sensor.PIPE_PRESURE_SENSOR, bytesToShortString(buf, 5));
                packets.add(packet);

                sensorId = gatewayId + "00010403";
                packet = new DataPacket(sensorId, Sensor.HIGHT_TEMPERATURE_SENSOR, bytesToShortString(buf, 7));
                packets.add(packet);

                sensorId = gatewayId + "00010404";
                packet = new DataPacket(sensorId, Sensor.DISPLACEMENT_SENSOR, bytesToShortString(buf, 9));
                packets.add(packet);
                break;
        }

        return packets;
    }

    private static List<DataPacket> parsePowerPacket(byte[] buf, String gatewayId) {
        List<DataPacket> packets = new ArrayList<DataPacket>();
        DataPacket packet;
//		Log.d("AAAAA", "DDDDD:" + Arrays.toString(buf));
        int a, b, c, d;
        float powerValue, electricValue;
        String sensorValue, sensorId = gatewayId + "00000055";
        String powerValuestr = null, electricValuestr = null;

        // 接收功率
        if (buf[9] == 7) {
            a = ((buf[16] > 0) ? buf[16] : (buf[16] + 256)) - 0x33;
            b = ((buf[15] > 0) ? buf[15] : (buf[15] + 256)) - 0x33;
            c = ((buf[14] > 0) ? buf[14] : (buf[14] + 256)) - 0x33;
            powerValuestr = String.format("%02x%02x.%02x", a, b, c);
            powerValue = Float.parseFloat(powerValuestr);
            powerValuestr = String.valueOf(powerValue);
            data.put("powerValuestr", powerValuestr);
//			Log.e("Datapacket", "功率：" + powerValuestr);
        }
        if (buf[9] == 8) {
            a = ((buf[17] > 0) ? buf[17] : (buf[17] + 256)) - 0x33;
            b = ((buf[16] > 0) ? buf[16] : (buf[16] + 256)) - 0x33;
            c = ((buf[15] > 0) ? buf[15] : (buf[15] + 256)) - 0x33;
            d = ((buf[14] > 0) ? buf[14] : (buf[14] + 256)) - 0x33;
            electricValuestr = String.format("%02x%02x%02x.%02x", a, b, c, d);
            electricValue = Float.parseFloat(electricValuestr);
            electricValuestr = String.valueOf(electricValue);
            data.put("electricValuestr", electricValuestr);
//			Log.e("Datapacket", "电量：" + electricValuestr);
        }

        if (data.get("powerValuestr") != null && data.get("electricValuestr") != null) {
            sensorValue = data.get("powerValuestr") + "," + data.get("electricValuestr");
            packet = new DataPacket(sensorId, Sensor.POWER_SENSOR, sensorValue);
//			Log.d("Datapacket", "DataPacket sensorType == " + Sensor.POWER_SENSOR + "   powerValue = " + data.get("powerValuestr")
//					+ "   electricValuestr = " + data.get("electricValuestr"));
            packets.add(packet);
        } else {
            Log.d("Datapacket", "DataPacket POWERMETER IS NULL");
        }

        return packets;
    }

    // 对zigbee数据，基本上一个传感器一个数据，仅有温湿度传感器特殊
    private static List<DataPacket> parseZigbeePacket(byte[] buf, String gatewayId) {
        List<DataPacket> packets = new ArrayList<DataPacket>();

        // 接收到的传感器类型。
        int sensorType = ByteIntConvertor.bytesToShort1(buf, 2);

        // 获取短地址，充当传感器的Id：网关Id+传感器Id
        // 传感器的id
        String sensorId = String.format("%s%02X%02X%02X%02X", gatewayId, buf[12], buf[13], buf[14], buf[15]);

        String valueStr;
        if (sensorType == Sensor.TEMP_WET_SENSOR) {
            // 获取传感器的值。此处先全部统一按同一种类型的数据处理
            int value = ByteIntConvertor.bytesHostToInt(buf, 16);
            valueStr = String.valueOf(value);
            // 获取传感器的值。此处先全部统一按同一种类型的数据处理
            value = ByteIntConvertor.bytesHostToInt(buf, 20);
            valueStr += "," + String.valueOf(value);
            Log.d("", "Dapacket Temp：value = " + valueStr + "  SensorId : " + sensorId);
        } else {
            // 获取传感器的值。此处先全部统一按同一种类型的数据处理
            int value = ByteIntConvertor.bytesHostToInt(buf, 16);
            valueStr = String.valueOf(value);
        }
        packets.add(new DataPacket(sensorId, sensorType, valueStr));
        Log.d("", "Dapacket rainDrop:type = " + sensorType + "  id:" + sensorId + "  valueStr:" + valueStr);
        return packets;
    }

    public String getSensorId() {
        return sensorId;
    }

    public int getSensorType() {
        return sensorType;
    }

    public String getSensorData() {
        return sensorData;
    }
}